package com.six.apidemic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.six.apidemic.R;
import com.six.apidemic.bean.PersonListCallbackBean;
import com.six.apidemic.databinding.ItemPersonBinding;
import com.six.apidemic.listener.BaseRecyclerViewHolder;
import com.six.apidemic.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员列表适配器
 * @author Administrator
 */
public class PersonAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<PersonListCallbackBean.ResultBean> strings = new ArrayList<>();
    private Context mcontext;
    public OnItemClickListener mOnItemClickLitener;

    public PersonAdapter(Context context, List<PersonListCallbackBean.ResultBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemPersonBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_person, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        TourViewHolder tv = (TourViewHolder) holder;

        //0:正常 1:封控 2:密接 3:次密接 4:阳性
        switch (strings.get(position).getStatus()){
            case "0":
                tv.tvModule.setText(strings.get(position).getName());
                tv.tvModule.setBackgroundColor(mcontext.getResources().getColor(R.color.white));
                break;
            case "1":
                tv.tvModule.setBackgroundColor(mcontext.getResources().getColor(R.color.red_f54a51));
                break;
            case "2":
                tv.tvModule.setText(strings.get(position).getName()+"（密接）");
                tv.tvModule.setBackgroundColor(mcontext.getResources().getColor(R.color.red_f54a51));
                break;
            case "3":
                tv.tvModule.setText(strings.get(position).getName()+"（次密接）");
                tv.tvModule.setBackgroundColor(mcontext.getResources().getColor(R.color.color_eb992f));
                break;
            case "4":
                tv.tvModule.setText(strings.get(position).getName()+"（确诊患者）");
                tv.tvModule.setBackgroundColor(mcontext.getResources().getColor(R.color.red_f54a51));
                break;
            default:
        }

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<PersonListCallbackBean.ResultBean> {
        ItemPersonBinding binding;
        TextView tvModule;

        public TourViewHolder(ItemPersonBinding dataBinding) {
            super(dataBinding.getRoot());
            binding = dataBinding;
            tvModule = dataBinding.tvModule;
        }

        @Override
        public void bindData(PersonListCallbackBean.ResultBean data) {
            binding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

}